package com.open.capacity.finance.util;


import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.open.capacity.finance.dto.SrzcbDto;
import com.open.capacity.utils.StringUtil;

import java.util.Optional;

public class PdfSrzcb {
    // 定义全局的字体静态变量
    private static Font titlefont;
    private static Font headfont;
    private static Font keyfont;
    private static Font textfont;

    private static Font text2font;

    // 最大宽度
    private static int maxWidth = 520;

    // 静态代码块
    static {
        try {
            // 不同字体（这里定义为同一种字体：包含不同字号、不同style）
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            titlefont = new Font(bfChinese, 16, Font.BOLD);
            headfont = new Font(bfChinese, 10, Font.NORMAL);
            keyfont = new Font(bfChinese, 10, Font.NORMAL);
            textfont = new Font(bfChinese, 10, Font.NORMAL);

            text2font = new Font(bfChinese, 12, Font.NORMAL);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    // 生成PDF文件
    public void generatePDF(Document document, String ztName, String pzrq, SrzcbDto data, float colWidths[]) throws Exception {
        Paragraph paragraphTitle = new Paragraph("收入支出表", titlefont);
        paragraphTitle.setAlignment(1); //设置文字居中 0靠左   1，居中     2，靠右
        paragraphTitle.setIndentationLeft(12); //设置左缩进
        paragraphTitle.setIndentationRight(12); //设置右缩进
        paragraphTitle.setLeading(20f); //行间距
        paragraphTitle.setSpacingBefore(45f); //设置段落上空白
        paragraphTitle.setSpacingAfter(30f); //设置段落下空白
        document.add(paragraphTitle);

        PdfPTable subtitle = new PdfPTable(3);
        subtitle.setTotalWidth(PageSize.A4.getWidth() - 68);
        subtitle.setWidths(new int[]{40, 20, 40});
        subtitle.setLockedWidth(true);
        subtitle.getDefaultCell().setFixedHeight(-10);
        subtitle.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        subtitle.addCell(new Paragraph("账套：" + ztName, iTextPDFUtil.getColorFont(BaseColor.BLACK, 10, "宋体")));
        subtitle.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        subtitle.addCell(new Paragraph(pzrq.substring(0, 4) + "年" + pzrq.substring(5, 7) + "月" + pzrq.substring(8, 10) + "日", iTextPDFUtil.getColorFont(BaseColor.BLACK, 10, "宋体")));
        subtitle.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
        subtitle.addCell(new Paragraph("单位:元", iTextPDFUtil.getColorFont(BaseColor.BLACK, 10, "宋体")));
        document.add(subtitle);

        PdfPTable table2 = createTable(colWidths);
        table2.setSpacingBefore(8f);//表格前间距


        table2.addCell(createCell("项目", textfont));
        table2.addCell(createCell("本月数", textfont));
        table2.addCell(createCell("本年累计数", textfont));

        table2.addCell(createCell("一、收入", text2font, Element.ALIGN_LEFT));
        table2.addCell(createCell("", textfont));
        table2.addCell(createCell("", textfont));

        table2.addCell(createCell("401 会费收入", textfont, Element.ALIGN_LEFT));
        Optional<SrzcbDto.ItemValue> value = data.getList().stream().filter(t -> t.getKjkmCode().equals("401")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("402 拨缴经费收入", textfont, Element.ALIGN_LEFT));
        ;
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("402")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("403 上级补助收入", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("403")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      40301 回拨补助", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("40301")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      40302 专项补助", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("40302")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      40303 超收补助", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("40303")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      40304 帮扶补助", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("40304")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      40305 送温暖补助", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("40305")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      40306 救灾补助", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("40306")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("404 政府补助收入", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("404")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("405 行政补助收入", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("405")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("406 事业收入", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("406")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("407 投资收益", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("407")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("408 其他收入", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("408")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("本期收入合计", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("sr_total")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("二、支出", text2font, Element.ALIGN_LEFT));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("501 职工活动支出", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("501")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50101 职工教育费", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50101")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50102 文体活动费", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50102")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50103 宣传活动费", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50103")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50104 其他活动支出", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50104")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));


        table2.addCell(createCell("502 维权支出", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("502")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50201 劳动关系协调费", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50201")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50202 劳动保护费", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50202")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50203 法律援助费", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50203")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50204 困难职工帮扶费", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50204")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50205 送温暖费", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50205")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50206 其他维权支出", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50206")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("503 业务支出", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("503")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50301 培训费", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50301")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50302 会议费", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50302")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50303 外事费", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50303")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50304 专项业务费", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50304")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50305 其他业务支出", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50305")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("504 行政支出", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("504")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50401 工资福利支出", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50401")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50402 商品和服务支出", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50402")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50403 对个人和家庭的补助", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50403")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50404 其他行政支出", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50404")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("505 资本性支出", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("505")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50501 房屋建筑物购建", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50501")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50502 办公设备购置", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50502")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50503 专用设备购置", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50503")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50504 交通工具购置", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50504")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50505 大型修缮", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50505")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50506 信息网络购建", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50506")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50507 其他资本性支出", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50507")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("506 补助下级支出", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("506")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50601 回拨补助", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50601")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50602 专项补助", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50602")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50603 超收补助", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50603")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50604 帮扶补助", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50604")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50605 送温暖补助", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50605")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50606 救灾补助", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50606")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("      50607 其他补助", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("50607")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("507 事业支出", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("507")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("508 其他支出", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("508")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("        本期支出合计", textfont, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("zc_total")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        table2.addCell(createCell("三、本期结余", text2font, Element.ALIGN_LEFT));
        value = data.getList().stream().filter(t -> t.getKjkmCode().equals("jy_total")).findFirst();
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getByfs().toString()) : "", textfont));
        table2.addCell(createCell(value.isPresent() ? StringUtil.fmtMicrometer(value.get().getBnlj().toString()) : "", textfont));

        document.add(table2);

        PdfPTable footertitle = new PdfPTable(2);
        footertitle.setTotalWidth(PageSize.A4.getWidth() - 68);
        footertitle.setWidths(new int[]{50, 50});
        footertitle.setLockedWidth(true);
        footertitle.getDefaultCell().setFixedHeight(-10);
        footertitle.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        footertitle.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        footertitle.addCell(new Paragraph("财务负责人：" + data.getCwzg(), iTextPDFUtil.getColorFont(BaseColor.BLACK, 10, "宋体")));
        footertitle.addCell(new Paragraph("制表人:" + data.getZdr(), iTextPDFUtil.getColorFont(BaseColor.BLACK, 10, "宋体")));
        document.add(subtitle);
    }


/**------------------------创建表格单元格的方法start----------------------------*/
    /**
     * 创建单元格(指定字体)
     *
     * @param value
     * @param font
     * @return
     */
    public PdfPCell createCell(String value, Font font) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setPhrase(new Phrase(value, font));
        cell.setPaddingBottom(5f);
        cell.setPaddingTop(5f);
        return cell;
    }

    /**
     * 创建单元格（指定字体、水平..）
     *
     * @param value
     * @param font
     * @param align
     * @return
     */
    public PdfPCell createCell(String value, Font font, int align) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setPhrase(new Phrase(value, font));
        cell.setPaddingBottom(5f);
        cell.setPaddingTop(5f);
        return cell;
    }

    /**
     * 创建单元格（指定字体、水平居..、单元格跨x列合并）
     *
     * @param value
     * @param font
     * @param align
     * @param colspan
     * @return
     */
    public PdfPCell createCell(String value, Font font, int align, int colspan) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }

    /**
     * 创建单元格（指定字体、水平居..、单元格跨x列合并、设置单元格内边距）
     *
     * @param value
     * @param font
     * @param align
     * @param colspan
     * @param boderFlag
     * @return
     */
    public PdfPCell createCell(String value, Font font, int align, int colspan, boolean boderFlag) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setPhrase(new Phrase(value, font));
        cell.setPadding(3.0f);
        if (!boderFlag) {
            cell.setBorder(0);
            cell.setPaddingTop(15.0f);
            cell.setPaddingBottom(8.0f);
        } else if (boderFlag) {
            cell.setBorder(0);
            cell.setPaddingTop(0.0f);
            cell.setPaddingBottom(15.0f);
        }
        return cell;
    }

    /**
     * 创建单元格（指定字体、水平..、边框宽度：0表示无边框、内边距）
     *
     * @param value
     * @param font
     * @param align
     * @param borderWidth
     * @param paddingSize
     * @param flag
     * @return
     */
    public PdfPCell createCell(String value, Font font, int align, float[] borderWidth, float[] paddingSize, boolean flag) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setPhrase(new Phrase(value, font));
        cell.setBorderWidthLeft(borderWidth[0]);
        cell.setBorderWidthRight(borderWidth[1]);
        cell.setBorderWidthTop(borderWidth[2]);
        cell.setBorderWidthBottom(borderWidth[3]);
        cell.setPaddingTop(paddingSize[0]);
        cell.setPaddingBottom(paddingSize[1]);
        if (flag) {
            cell.setColspan(2);
        }
        return cell;
    }
/**------------------------创建表格单元格的方法end----------------------------*/


/**--------------------------创建表格的方法start------------------- ---------*/
    /**
     * 创建默认列宽，指定列数、水平(居中、右、左)的表格
     *
     * @param colNumber
     * @param align
     * @return
     */
    public PdfPTable createTable(int colNumber, int align) {
        PdfPTable table = new PdfPTable(colNumber);
        try {
            table.setTotalWidth(maxWidth);
            table.setLockedWidth(true);
            table.setHorizontalAlignment(align);
            table.getDefaultCell().setBorder(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return table;
    }

    /**
     * 创建指定列宽、列数的表格
     *
     * @param widths
     * @return
     */
    public PdfPTable createTable(float[] widths) {
        PdfPTable table = new PdfPTable(widths);
        try {
            table.setTotalWidth(maxWidth);
            table.setLockedWidth(true);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.getDefaultCell().setBorder(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return table;
    }

    /**
     * 创建空白的表格
     *
     * @return
     */
    public PdfPTable createBlankTable() {
        PdfPTable table = new PdfPTable(1);
        table.getDefaultCell().setBorder(0);
        table.addCell(createCell("", keyfont));
        table.setSpacingAfter(20.0f);
        table.setSpacingBefore(20.0f);
        return table;
    }
/**--------------------------创建表格的方法end------------------- ---------*/
}
